package com.uppfind.framework.core.service.impl;

import com.uppfind.framework.core.dao.api.QueryMapper;
import com.uppfind.framework.core.dao.api.common.params.PagerResult;
import com.uppfind.framework.core.dao.api.common.params.QueryParam;
import com.uppfind.framework.core.service.QueryService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xuliugen on 2017/6/29.
 */
public interface SimpleQueryService<PO, PK> extends QueryService<PO, PK> {

    QueryMapper<PO, PK> getQueryMapper();

    @Override
    default PagerResult<PO> selectPager(QueryParam param) {
        param.setForTotal(true);
        int count = getQueryMapper().total(param);
        if (count != 0) {
            PagerResult<PO> pagerResult = new PagerResult<>();
            pagerResult.setTotal(count);
            param.setForTotal(false);
            List<PO> result = getQueryMapper().select(param);
            pagerResult.setData(result);
            return pagerResult;
        }
        return null;
    }

    @Override
    @Transactional(readOnly = true)
    default List<PO> select(QueryParam param) {
        return getQueryMapper().select(param);
    }

    @Override
    @Transactional(readOnly = true)
    default int total(QueryParam param) {
        return getQueryMapper().total(param);
    }

    @Override
    @Transactional(readOnly = true)
    default PO selectByPk(PK pk) {
        return getQueryMapper().selectByPk(pk);
    }

    @Override
    @Transactional(readOnly = true)
    default List<PO> select() {
        return null;
    }

}
